<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" :xs="0" class="left">
      <div>
        <div class="font-bold text-4xl text-light-50 mb-4">
          商铺综合管理系统
        </div>
        <div class="text-gray-200 text-lg">基于vue3+vite+elementplus</div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" :xs="24" class="right">
      <h2 class="font-bold text-3xl text-gray-800">欢迎访问</h2>
      <div
        class="flex items-center justify-center my-5 text-gray-400 space-x-2"
      >
        <span class="h-[1px] w-16 bg-gray-200"></span>
        <span>账号密码登录</span>
        <span class="h-[1px] w-16 bg-gray-200"></span>
      </div>
      <el-form ref="formRef" :rules="rules" :model="form" class="w-[250px]">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix
              ><el-icon><Lock /></el-icon
            ></template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-[250px]"
            color="#0EA5E9"
            round
            @click="onSubmit"
            @keydown.enter="keyDown()"
            :loading="loading"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted,onBeforeMount } from "vue";
import { toast } from "~/composables/util";
import { useRouter } from "vue-router";
import { useStore } from "vuex";


const router = useRouter();
const store = useStore();

const form = reactive({
  username: "",
  password: "",
});
const rules = {
  username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
};

const formRef = ref(null);
const loading = ref(false);
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return false;
    }
    loading.value = true;

    store.dispatch("login", form).then((res) => {
        // 提示成功
        toast("登录成功");
        // 跳转到后台首页
        router.push("/");
    }).finally(() => {
        loading.value = false;
    });
  });
};

onMounted(() => {
  // 绑定监听事件
  window.addEventListener("keydown", keyDown);
});
onUnmounted(() => {
  // 销毁事件
  window.removeEventListener("keydown", keyDown, false);
});
// 点击回车键登录
const keyDown = (e) => {
  if (e.keyCode === 13 || e.keyCode === 100) {
    console.log("触发回车事件");
    onSubmit();
  }
};
</script>
<style>
.login-container {
  @apply min-h-screen bg-sky-500;
}
.login-container .left,
.login-container .right {
  @apply flex items-center justify-center;
}
.login-container .right {
  @apply bg-light-50 flex-col;
}
</style>
